﻿using System.Collections.Generic;
namespace SanguoSharp.Server
{
    /// <summary>
    /// 桌面上的牌(装备牌.....)
    /// </summary>
    public class TableCard : PlayableCard
    {
        private bool playBlocked;

        protected TableCard(Game game, int id, CardType type, CardSuit suit, CardRank rank)
            : base(game, id, type, suit, rank)
        {
            playBlocked = false;
        }

        /// <summary>
        ///  回合结束
        /// </summary>
        public void OnTurnEnded()
        {
            if (IsOnTable)
                playBlocked = false;
        }

        /// <summary>
        /// 出牌阶段 对于装备拍来说...就是使用吧
        /// </summary>
        protected override void OnPlay()
        {
            if (IsInHand)
            {
                List<TableCard> tableCopy = new List<TableCard>(Owner.Table);
                foreach (TableCard c in tableCopy)
                    if (c.Type == Type)
                        //用牌
                        Game.GameTable.PlayerDiscardCard(c);
                    else if (c.IsWeapon && IsWeapon)
                        //换武器
                        Game.GameTable.PlayerDiscardCard(c);
                Game.GameTable.PlayerPlayCardOnTable(this);
                playBlocked = true;
            }
            else
            {
                if (playBlocked)
                    throw new BadUsageException();
                PlayFromTable();
            }
        }
        protected override void OnPlayVirtually(Card card)
        {
            PlayFromTableVirtually(card);
        }
        protected virtual void PlayFromTable()
        {
            throw new BadUsageException();
        }
        protected virtual void PlayFromTableVirtually(Card card)
        {
            throw new BadUsageException();
        }

        public override void CheckMissed(CardResultMethod resultMethod)
        {
            if (IsOnTable && !playBlocked)
                OnCheckMissed(resultMethod);
            else
                throw new BadCardException();
        }
        protected virtual void OnCheckMissed(CardResultMethod resultMethod)
        {
            throw new BadCardException();
        }

        public void PredrawCheck()
        {
            AssertOnTable();
            OnPredrawCheck();
        }
        protected virtual void OnPredrawCheck()
        {
            throw new BadUsageException();
        }
    }
}

